package com.project.action.product;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartResolver;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.project.action.user.UserAction;
import com.project.service.ProductService;
import com.project.util.DateUtil;
import com.project.util.Fileupload;
import com.project.util.SendMail;
import com.project.util.StringUtil;
import com.project.vo.Picture;
import com.project.vo.Product;
import com.project.vo.User;
import com.sun.org.apache.bcel.internal.generic.NEW;

import net.sf.json.JSONObject;

public class ProductAction extends ActionSupport{
	
	private Integer id;
	private String description;
	private String name;
	private Integer kind_id;
	private Float price;
	private Float preprice;
	private Integer num;
	private String url;
//	private List pictures;
	private Integer user_id;
	private String user_name;
	private ProductService productService;
	private MultipartResolver multipartResolver;
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Picture picture;
	private Integer picture_id;
	private List<File> pictures = new ArrayList<File>();
	private List<String> picturesContentType = new ArrayList<>();
	private List<String> picturesFileName = new ArrayList<>();
	private String json;
	//分页
	Integer pageNum;
	
	public void getContext() {
		// TODO Auto-generated constructor stub
		ActionContext context = ActionContext.getContext();
		request = (Map<String, Object>) context.get(StringUtil.REQUEST);
		session = context.getSession();
	}
	public HttpServletRequest getHttpRequest(){
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		return request;
	}
	public User getUserBySession(){
		User user = new User();
		user = (User)session.get(StringUtil.USER);
		return user;
	}
	
	public String add() {
		getContext();
		String result = StringUtil.FAILURE;
		//得到用户所属的产品的数量
//		Integer productNum = productService.getCountUserProduct(user_id) + 1;
		Product product = new Product();
		product.setDescription(description);
		product.setName(name);
		product.setKind_id(kind_id);
		product.setNum(num);
		product.setPrice(price);
		product.setPreprice(preprice);
		product.setUrl(url);
		product.setUser_id(user_id);
		product.setDate(DateUtil.stringToDate(DateUtil.getCurrentDate()));
		product.setYear(DateUtil.getCurrentYear());
		product.setMonth(DateUtil.getCurrentMonth());
//		//需要根据用户id得到商品的数量，作为文件名的一部分。
//		String dir = StringUtil.F_SAVE_DIRECTORY+"/"+ getUserBySession().getId()+ "/" +productNum+"/";
//		String loadRes = Fileupload.uploadFiles(dir, pictures, picturesFileName);
//		if (!loadRes.equals(StringUtil.SUCCESS)) {
//			return StringUtil.FAILURE;
//		}
//		String savePath = StringUtil.F_SAVE_PATH +"/"+ getUserBySession().getId()+ "/" +productNum+"/";
//		List<String> localPath = Fileupload.getLocalPaths(picturesFileName, savePath);
		//处理图片
//		Set pic = new HashSet();
//		for (int i = 0; i < picturesFileName.size(); i++) {
//			Picture picture = new Picture();
//			picture.setPath(picturesFileName.get(i));
//			pic.add(picture);
//		}
//		product.setPictures(pic);
		result = productService.add(product, pictures, picturesFileName);
		if(result.equals(StringUtil.SUCCESS)){
			return viewAll();
		}else{
			return StringUtil.FAILURE;
		}
		
	}

	public String viewAll() {
		getContext();
		if (user_id == null) {
			User user = getUserBySession();
			user_id = user.getId();
		}
		if (null == pageNum) {
			pageNum = 1;
		}
		List<Product> products = new ArrayList<Product>();
		List<Product> allProducts = new ArrayList<Product>();
		if (user_id != 0) {
			allProducts = productService.queryAllByUserId(user_id);
			products = productService.queryAllByUserId(user_id, StringUtil.PAGESIZE, pageNum);
		}else {
			allProducts = productService.queryAllProduct();
			products = productService.queryAllProduct(pageNum, StringUtil.PAGESIZE);
		}
		request.put(StringUtil.PRODUCTS, products);
		request.put(StringUtil.COUNTNUM, allProducts.size());
		return StringUtil.SUCCESS;
	}
	
	public String pageChange(){
		getContext();
		Map<String, Object> map = new HashMap<>();
		List<Product> products = new ArrayList<Product>();
		if (user_id == null) {
			User user = getUserBySession();
			user_id = user.getId();
		}
		if (null == pageNum) {
			pageNum = 1;
		}
		if (user_id != 0) {
			products = productService.queryAllByUserId(user_id, StringUtil.PAGESIZE, pageNum);
		}else{
			products = productService.queryAllProduct(pageNum, StringUtil.PAGESIZE);
		}
		map.put(StringUtil.RESULT, products);
		JSONObject  jsonObject = JSONObject.fromObject(map);
		json = jsonObject.toString();
		return StringUtil.SUCCESS;
	}
	
	public String view() {
		getContext();
		String result = StringUtil.FAILURE;
		Product product = productService.queryById(id);
		List<Picture> pictures = productService.queryPictureByProductId(product.getId());
		if(product != null){
			request.put(StringUtil.PICTURES, pictures);
			request.put(StringUtil.PRODUCT, product);
			result = StringUtil.SUCCESS;
		}
		return result;
	}
	public String delete() {
		String result = StringUtil.FAILURE;
		result = productService.delete(id);
		if (result.equals(StringUtil.FAILURE)) {
			return result;
		}
		return viewAll();
	}
	public String imageDelete() {
		getContext();
		String result = StringUtil.FAILURE;
		result = productService.imageDelete(picture_id);
		if (result.equals(StringUtil.FAILURE)) {
			return result;
		}
		//组装json数据
		Map<String, Object> map = new HashMap<>();
		map.put(StringUtil.RESULT, result);
		JSONObject jsonObject = JSONObject.fromObject(map);
		json = jsonObject.toString(); 	//json必须有setter和getter
		return StringUtil.SUCCESS;
	}
	public String edit() {
		getContext();
		Product product = productService.queryById(id);
		List<Picture> pictures = productService.queryPictureByProductId(product.getId());
		if(product != null){
			request.put(StringUtil.PICTURES, pictures);
			request.put(StringUtil.PRODUCT, product);
			return StringUtil.SUCCESS;
		}
		return StringUtil.FAILURE;
	}
	
	public String update() {
		getContext();
		Product product = productService.queryById(id);
		product.setName(name);
		product.setKind_id(kind_id);
		product.setDescription(description);
		product.setNum(num);
		product.setPrice(price);
		product.setUrl(url);
		product.setDate(DateUtil.stringToDate(DateUtil.getCurrentDate()));
		product.setYear(DateUtil.getCurrentYear());
		product.setMonth(DateUtil.getCurrentMonth());
//		//处理图片
//		Set pic = new HashSet();
//		//需要根据用户id得到商品的数量，作为文件名的一部分。
//		String dir = StringUtil.F_SAVE_DIRECTORY+"/"+ getUserBySession().getId()+ "/" +product.getId()+"/";
//		String loadRes = Fileupload.uploadFiles(dir, pictures, picturesFileName);
//		if (!loadRes.equals(StringUtil.SUCCESS)) {
//			return StringUtil.FAILURE;
//		}
//		String savePath = StringUtil.F_SAVE_PATH +"/"+ getUserBySession().getId()+ "/" +product.getId()+"/";
//		List<String> localPath = Fileupload.getLocalPaths(picturesFileName, savePath);
//		for (int i = 0; i < localPath.size(); i++) {
//			Picture picture = new Picture();
//			picture.setPath(localPath.get(i));
//			picture.setProduct_id(product.getId());
//			pic.add(picture);
//		}
		String result = productService.update(product, pictures, picturesFileName);
		if (result.equals(StringUtil.FAILURE)) {
			return StringUtil.FAILURE;
		} else {
			return view();
		}
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public Integer getNum() {
		return num;
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List getPictures() {
		return pictures;
	}

	public Integer getPageNum() {
		return pageNum;
	}
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	public Integer getUser_id() {
		return user_id;
	}

	public List<String> getPicturesContentType() {
		return picturesContentType;
	}
	public void setPicturesContentType(List<String> picturesContentType) {
		this.picturesContentType = picturesContentType;
	}
	public List<String> getPicturesFileName() {
		return picturesFileName;
	}
	public void setPicturesFileName(List<String> picturesFileName) {
		this.picturesFileName = picturesFileName;
	}
	public void setPictures(List<File> pictures) {
		this.pictures = pictures;
	}
	public void setUser_id(Integer user_id) {
		this.user_id = user_id;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public MultipartResolver getMultipartResolver() {
		return multipartResolver;
	}
	public void setMultipartResolver(MultipartResolver multipartResolver) {
		this.multipartResolver = multipartResolver;
	}
	public List<File> getFiles() {
		return pictures;
	}
	public void setFiles(List<File> pictures) {
		this.pictures = pictures;
	}
	public Picture getPicture() {
		return picture;
	}
	public void setPicture(Picture picture) {
		this.picture = picture;
	}
	public Integer getPicture_id() {
		return picture_id;
	}
	public void setPicture_id(Integer picture_id) {
		this.picture_id = picture_id;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public Float getPreprice() {
		return preprice;
	}
	public void setPreprice(Float preprice) {
		this.preprice = preprice;
	}
	public Integer getKind_id() {
		return kind_id;
	}
	public void setKind_id(Integer kind_id) {
		this.kind_id = kind_id;
	}
}
